package com.gump.essay.juc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @auther zzc
 * @date 2021 04 18
 */
public class 资源类 {

    private int number = 0;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void add() {

        lock.lock();
        try {
            //1 判断
            while (number != 0) {
                //阻塞  等待   不能生产
                condition.await();
            }
            //2 干活
            number++;
            System.out.println(Thread.currentThread().getName()+"\t"+number);
            //3 通知
            condition.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void remove() {
        lock.lock();
        try {
            //1 判断
            while (number == 0) {
                //阻塞  等待   不能生产
                condition.await();
            }
            //2 干活
            number--;
            System.out.println(Thread.currentThread().getName()+"\t"+number);
            //3 通知
            condition.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
